package com.kaede.hayate.gen;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenerateHayate {
	
	//生成的模块名（所有生成的文件在该目录下）
	public static final String MODULE_NAME = "sys";
	
	//数据库表前缀（如果不填生成的实体会包含前缀）
	public static final String[] TABLE_PREFIX = {"sys_"};
	
	//要生成哪些表
	public static final String[] INCLUDE_TABLES = 
		{"sys_user_role", "sys_role_authority"};
	
	
	public static void generateByTableName() {

        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();
        
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(GenCommonConfig.OUTPUT_DIR);//输出目录
        globalConfig.setFileOverride(true);// 是否覆盖文件
        globalConfig.setActiveRecord(false);// 开启 activeRecord 模式
        globalConfig.setEnableCache(false);// XML 二级缓存
        globalConfig.setBaseResultMap(true);// XML ResultMap
        globalConfig.setBaseColumnList(true);// XML columList
        globalConfig.setAuthor(GenCommonConfig.AUTHOR);
        autoGenerator.setGlobalConfig(globalConfig);
        
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);// 数据库类型
        dataSourceConfig.setTypeConvert(new MySqlTypeConvert() {
            // 自定义数据库表字段类型转换【可选】
            @Override
            public DbColumnType processTypeConvert(String fieldType) {
                System.out.println("转换类型：" + fieldType);
                 if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
                    return DbColumnType.BOOLEAN;
                 }
                return super.processTypeConvert(fieldType);
            }
        });
        dataSourceConfig.setDriverName(GenCommonConfig.DRIVER_NAME);
        dataSourceConfig.setUsername(GenCommonConfig.USERNAME);
        dataSourceConfig.setPassword(GenCommonConfig.PASSWORD);
        dataSourceConfig.setUrl(GenCommonConfig.URL);
        autoGenerator.setDataSource(dataSourceConfig);
        
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setTablePrefix(TABLE_PREFIX);// 此处可以修改为您的表前缀
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategyConfig.setInclude(INCLUDE_TABLES); // 需要生成的表
        strategyConfig.setRestControllerStyle(true);//是否@RestController
        strategyConfig.setControllerMappingHyphenStyle(true);//@RequestMapping("/managerUserActionHistory")->@RequestMapping("/manager-user-action-history")
        autoGenerator.setStrategy(strategyConfig);
        
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName(MODULE_NAME);// 生成的模块名（所有生成的文件在该目录下）
        packageConfig.setParent(GenCommonConfig.PACKAGE_NAME);// 自定义包路径
        packageConfig.setController(GenCommonConfig.CONTROLLER_PACKAGE_NAME);// 这里是控制器包名，默认 web
        autoGenerator.setPackageInfo(packageConfig);

        // 执行生成
        autoGenerator.execute();
    }

	public static void main(String[] args) {
		GenerateHayate.generateByTableName();
	}
}
